package com.play.airfight.game_view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import com.play.airfight.R;
import com.play.airfight.GlobalInfo;
import com.play.airfight.managers.GameManager;
import com.play.airfight.managers.Player;

public class PlaneGameView extends SurfaceView implements SurfaceHolder.Callback {

    private MgameThread mGameThread;
    private boolean mSurfaceWorking = false;
    private GameManager mGameManager;
    private SurfaceHolder mSfcHolder;

    public PlaneGameView(Context context) {
        super(context);
        mSfcHolder = this.getHolder();
        mSfcHolder.addCallback(this);
//        setBackgroundColor(getResources().getColor(R.color.white))
        this.setZOrderOnTop(true);
        this.getHolder().setFormat(PixelFormat.TRANSLUCENT);

    }

    private static PlaneGameView planeGameView;

    // 懒汉单例
    public static PlaneGameView getPlaneGameView(Context context) {
        if (planeGameView == null) {
            planeGameView = new PlaneGameView(context);
        }
        planeGameView.setFocusable(true);
        return planeGameView;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
//        mSfcHolder = holder;
        GlobalInfo.screenW = getWidth();
        GlobalInfo.screenH = getHeight();
        mSurfaceWorking = true;

        mGameManager = new GameManager(getContext());

        mGameThread = new MgameThread();
        mGameThread.start();
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {

    }


    Bitmap bitmapCache;

    public void drawFrame() {
        Canvas canvas = null;

        try {
            canvas = mSfcHolder.lockCanvas();
            if (canvas == null) {
                return;
            }

            if (bitmapCache == null) {
                bitmapCache = BitmapFactory.decodeResource(getResources(), R.drawable.fight_area);
            }
//            Canvas canvasCache = new Canvas(bitmapCache);
//            drawCache(canvasCache);
//            Paint p = new Paint();
//            // 清屏
//            p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
//            canvas.drawPaint(p);

            canvas.drawBitmap(bitmapCache, 0, 0, null); // 重新绘制背景的同时 ，清除 了双缓冲的绘制痕迹
            mGameManager.drawFrame(canvas);

        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            if (canvas != null) {
                mSfcHolder.unlockCanvasAndPost(canvas);
            }
        }
    }



    private void execLogic() {
        mGameManager.execLogic();
    }


    // 游戏总线程
    class MgameThread extends Thread {

        @Override
        public void run() {
            while (mSurfaceWorking) {
                long start = System.currentTimeMillis();

                drawFrame();    // 画帧！
                execLogic();    // 执行所有游戏逻辑！

                long end = System.currentTimeMillis();
                try {
                    if (end - start < 16) {
                        Thread.sleep(16 - (end - start));    // 睡眠合适的时间！
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                break;
            case MotionEvent.ACTION_MOVE:
                Player.currentX = (int) event.getX();
                Player.currentY = (int) event.getY();
                break;
//            case MotionEvent.ACTION_UP:
//                break;
            default:
                break;
        }

        // 触摸事件已消费
        return true;
    }
}
